﻿using System;
using System.Collections.Generic;
using System.Text;

namespace IllustratedCsharp7_ch18Generic
{
    class Animal
    {
        public int NumberofLegs = 4;
    }
    class Dog : Animal
    { }

    /*泛型委托 Factory
        接受一个类型参数T
        返回一个类型为T的对象
     */
    //delegate T Factory<T>();

    /*
        关键字out指定了类型参数的协变
        协变关系允许程序更高的派生类型处理返回及输出的位置
     */
    delegate T Factory< out T>();
    class Chapt18_10_Convariance
    {
       /// <summary>
       /// 产生一个Dog对象
       /// </summary>
       /// <returns>返回一个Dog对象</returns>
        static Dog MakeDog()        //方法匹配委托
        {
            return new Dog();
        }
        static void _Main()
        {
            Animal a1 = new Animal();
            Animal a2 = new Dog();
            Console.WriteLine($"Number of dog legs:{a2.NumberofLegs}");

            Factory<Dog> dogMaker = MakeDog;        //创建委托对象
            Factory<Animal> animalMaker = dogMaker; //尝试赋值委托对象

            Console.WriteLine(animalMaker().NumberofLegs.ToString());
        }
    }
}
